#include <SoftwareSerial.h>
#include <CapacitiveSensor.h>
#include <RTClib.h>
//DigitalPin 2,4,7,8,11,12,13,

SoftwareSerial BTSerial(0,1); //Connect HC-06 TX,RX 

CapacitiveSensor   cs_4_12 = CapacitiveSensor(2,12);        // 10M resistor between pins 2 & 4, pin 4 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_7 = CapacitiveSensor(2,7);        // 10M resistor between pins 2 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_4_8 = CapacitiveSensor(2,8);        // 10M resistor between pins 2 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_4_13 = CapacitiveSensor(2,13);        // 10M resistor between pins 2 & 8, pin 8 is sensor pin, add a wire and or foil

long  total_sum = 0; 
long   goal_sum = 0;

void setup()  
 {
   cs_4_12.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);

  // set the data rate for the BT port
   BTSerial.begin(9600);
 }

void loop()
 {
   char dute;
   
   if (BTSerial.available())
   {
     goal_sum = BTSerial.read();
     Serial.print("Goal Sum : ");
     Serial.println(goal_sum);
   } 
   if (Serial.available())
   {
     dute = Serial.read();
     BTSerial.print("Date : ");
     BTSerial.println(dute);
   }  

    long coin_10 =  cs_4_12.capacitiveSensor(30);
    long coin_50 =  cs_4_7.capacitiveSensor(30);
    long coin_100 =  cs_4_8.capacitiveSensor(30);  
    long coin_500 =  cs_4_13.capacitiveSensor(30);
    long  coin = 0;  
    
    Serial.print(coin_10);                  // print sensor output 1
    Serial.print("\t");
    Serial.print(coin_50);                  // print sensor output 2
    Serial.print("\t");
    Serial.print(coin_100);    // print sensor output 3
    Serial.print("\t");
    Serial.println(coin_500); 

    if(coin_10 != 0)
    {
      coin = 10;      
    }
    else if(coin_50 != 0)
    {
      coin = 50;  
    }
    else if(coin_100 != 0)
    {
      coin = 100;
    }
   else if(coin_500 != 0)
    {
      coin = 500;
    }
   else
   {
   }
   
   total_sum += coin;
   
   if(goal_sum != 0)
   {
      if ( total_sum >= goal_sum )
      {
         Serial.println("목표금액을 달성하셨습니다.");
         Serial.print(" Goal_sum : ");
         Serial.println(goal_sum);
         Serial.print(" Total sum: ");
         Serial.println(total_sum);
      }
   }
    delay(10);                             // arbitrary delay to limit data to serial port 
 }


